package com.cloud.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath*:META-INF/applicationContext-druid.xml")
public class DruidStatViewServletConfig {

    // druid监控登陆帐号
    @Value("${com.cloud.server.druid.loginUsername:admin}")
    protected String loginUsername;

    // druid监控登陆密码
    @Value("${com.cloud.server.druid.loginPassword:admin123}")
    protected String loginPassword;

    @Bean
    public StatViewServlet testServlet() {
        return new StatViewServlet();
    }

    @Bean
    public ServletRegistrationBean testServletRegistrationBean(
            StatViewServlet testServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                testServlet);
        registration.setEnabled(true);

        registration.addInitParameter("loginUsername", loginUsername);
        registration.addInitParameter("loginPassword", loginPassword);

        registration.addUrlMappings("/druid/*");
        return registration;
    }

    @Bean
    public FilterRegistrationBean registFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new WebStatFilter());
        registration.addInitParameter("profileEnable", "true");
        registration.addInitParameter("exclusions",
                "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        registration.addUrlPatterns("/*");
        registration.setOrder(1);
        return registration;
    }

}